import complex_query from '@dfeidao/atom-nodejs/db/complex-query';
import log from '@dfeidao/atom-nodejs/logger/log';
import parse from '@dfeidao/atom-web/dt/parse';
import { IncomingHttpHeaders } from 'http';
import { systemid } from '../../atom/config';
interface Message {
	filter: {
		plan_name: string;
		starttime: string;
		endtime: string;
		pageno: number;
		asc?: string;
		desc?: string;
	};
}
// 表名	implement_record_sheet
// 标题	实施记录表
// 字段名称	字段标题	字段类型	字段长度	是否为空	是否主键
// _id	id	string	50	ⅹ	√
// proj_role_id	任务编号	string	50	√	ⅹ
// record_no	记录编号	string	50	√	ⅹ
// inspection_time	检查时间	timestamp	50	√	ⅹ
// plan_name	计划名称	string	50	√	ⅹ
// record_location	记录地点	string	500	√	ⅹ
// productid	产品ID	string	50	√	ⅹ
// plan_no	计划编号	string	50	√	ⅹ

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:d:\a飞道考核\kh\src\imple-records\zj-000004\s001,action_id:' + action_id);
	const q = complex_query(action_id, session_id, systemid);
	// 1.1依据   “产品ID productid"表中的“产品ID  productid ”作为条件取<实施记录表>中的数据
	const judge = '%' + message.filter.plan_name + '%';
	const a = message.filter.asc;
	const b = message.filter.desc;
	let data = [];
	if (a) {
		data = await q
			// 添加字段
			.add_field('implement_record_sheet', 'record_no', 'record_no')
			.add_field('implement_record_sheet', 'plan_no', 'plan_no')
			.add_field('implement_record_sheet', 'plan_name', 'plan_name')
			.add_field('implement_record_sheet', 'record_location', 'record_location')
			.add_field('implement_record_sheet', 'inspection_time', 'inspection_time')
			.where_like('implement_record_sheet', 'plan_name', judge)
			.where_gte('implement_record_sheet', 'inspection_time', parse(message.filter.starttime))
			.where_lte('implement_record_sheet', 'inspection_time', parse(message.filter.endtime))
			.orderby(a)
			.page(2, message.filter.pageno).exec();
	} else if (b) {
		data = await q
			// 添加字段
			.add_field('implement_record_sheet', 'record_no', 'record_no')
			.add_field('implement_record_sheet', 'plan_no', 'plan_no')
			.add_field('implement_record_sheet', 'plan_name', 'plan_name')
			.add_field('implement_record_sheet', 'record_location', 'record_location')
			.add_field('implement_record_sheet', 'inspection_time', 'inspection_time')
			.where_like('implement_record_sheet', 'plan_name', judge)
			.where_gte('implement_record_sheet', 'inspection_time', parse(message.filter.starttime))
			.where_lte('implement_record_sheet', 'inspection_time', parse(message.filter.endtime))
			.orderby_desc(b)
			.page(2, message.filter.pageno).exec();
	} else {
		data = await q
			// 添加字段
			.add_field('implement_record_sheet', 'record_no', 'record_no')
			.add_field('implement_record_sheet', 'plan_no', 'plan_no')
			.add_field('implement_record_sheet', 'plan_name', 'plan_name')
			.add_field('implement_record_sheet', 'record_location', 'record_location')
			.add_field('implement_record_sheet', 'inspection_time', 'inspection_time')
			.where_like('implement_record_sheet', 'plan_name', judge)
			.where_gte('implement_record_sheet', 'inspection_time', parse(message.filter.starttime))
			.where_lte('implement_record_sheet', 'inspection_time', parse(message.filter.endtime))
			.page(2, message.filter.pageno).exec();
	}

	const n = await q
		// 添加字段
		.add_field('implement_record_sheet', 'record_no', 'record_no')
		.add_field('implement_record_sheet', 'plan_no', 'plan_no')
		.add_field('implement_record_sheet', 'plan_name', 'plan_name')
		.add_field('implement_record_sheet', 'record_location', 'record_location')
		.add_field('implement_record_sheet', 'inspection_time', 'inspection_time')
		.where_like('implement_record_sheet', 'plan_name', judge)
		.where_gte('implement_record_sheet', 'inspection_time', parse(message.filter.starttime))
		.where_lte('implement_record_sheet', 'inspection_time', parse(message.filter.endtime))
		.exec();

	log('Service end path:d:\a飞道考核\kh\src\imple-records\zj-000004\s001,action_id:' + action_id);
	return {
		data: { data, n }
	};
}
